/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.BankDAO;
import com.ivanbiz.dao.impl.BankDAOImpl;
import com.ivanbiz.model.Bank;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Executions;

import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author iw20149x
 */
public class BankService extends Window {

    BankDAO bankDAO = new BankDAOImpl();
    Listbox listBank;

    public void onCreate() {
        listBank = (Listbox) getFellow("listBank");
        loadBank();
    }

    public void loadBank() {
        try {
            listBank.getItems().clear();
            List<Bank> loadBank = bankDAO.getAll(Bank.class);
            int no = 0;
            int x = 0;
            for (Bank bank : loadBank) {
                no += 1;
                Listitem item = new Listitem();
                item.setValue(bank);
                item.appendChild(new Listcell(String.valueOf(no)));
                item.appendChild(new Listcell(bank.getNama()));
                item.appendChild(new Listcell(bank.getDeskripsi()));
                listBank.appendChild(item);
                ++x;
            }
        } catch (Exception ex) {
            Logger.getLogger(BankService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void tambah() {
        Window win = (Window) Executions.createComponents("/bankAction.zul", null, null);
        win.doModal();
        loadBank();
    }

    public void ubah() {
        Bank bank = listBank.getSelectedItem().getValue();
        Window win = (Window) Executions.createComponents("/bankAction.zul", null, null);
        win.setTitle("Ubah Bank");
        win.setAttribute("bank", bank);
        Textbox nama = (Textbox) win.getFellow("nama");
        nama.setValue(bank.getNama());
        Textbox deskripsi = (Textbox) win.getFellow("deskripsi");
        deskripsi.setValue(bank.getDeskripsi());
        win.doModal();
        loadBank();
    }

    public void hapus() {
        try {
            Bank bank = listBank.getSelectedItem().getValue();
            bankDAO.delete(bank);
            loadBank();
        } catch (Exception ex) {
            Logger.getLogger(BankService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
